package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.getRebateRateByIdListBean;
import com.ymatou.iapi.godmch.service.getRebateRateByIdListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*代理类型id批量查询返佣比例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getRebateRateByIdList{
	private static getRebateRateByIdListBean bean;
	private static getRebateRateByIdListCall call;
	public agent_typeWapper agentTypeWapper = new agent_typeWapper();
	mch_relationWapper mch_relationwapeper=new mch_relationWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代理类型id批量查询返佣比例");
	}

	@Before
	public void caseUp() {
		bean=new getRebateRateByIdListBean();
		call=new getRebateRateByIdListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify(List<Integer>  list) throws InterruptedException, JSONException
	{
		Logger.verifyEquals("200", call.getStatus1(), "验证返回的status");
		JSONArray obj=call.getRebateRateList();
		
		int i=0;
		for(Integer typeId:list)
		{
		  Logger.comment("agentTypeId:"+typeId);
		   Map<String, Object> map = new HashMap();
		   map.put("agentType", typeId);
		
		  agent_type type = agentTypeWapper.selectby(map).get(0);
		  Logger.verifyEquals(new BigDecimal(obj.getJSONObject(i).getDouble("rebateRate")).stripTrailingZeros().toPlainString(),type.getRebateRate().stripTrailingZeros().toPlainString(), "验证返回的佣金");
		  i++;
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getRebateRateByIdList_001() {
		Logger.start(true,"代理类型id批量查询返佣比例-验证代理全是自主选货,无返佣");
		try {
			createmchBean createmchbean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			int mchId = createmchbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(1);
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			HashMap map=new HashMap();
			map.put("mchId", mchId);
			List<agent_type> list=agentTypeWapper.selectby(map);
				
			bean.setAgentTypeIdList(Lists.newArrayList(list.get(0).getAgentType()));
			call.setData(bean);
			call.callService();			
			
			check_verify( bean.getAgentTypeIdList());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getRebateRateByIdList_002() {
		Logger.start(true,"代理类型id批量查询返佣比例-验证代理包含自主继承,有返佣");
		try {
			createmchBean createmchbean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			int mchId = createmchbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(2);
			createagentBean.setRebateRate(new BigDecimal(22));
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			HashMap map=new HashMap();
			map.put("mchId", mchId);
			List<agent_type> list=agentTypeWapper.selectby(map);
				
			bean.setAgentTypeIdList(Lists.newArrayList(list.get(0).getAgentType(),list.get(1).getAgentType()));
			call.setData(bean);
			call.callService();			
			
			check_verify( bean.getAgentTypeIdList());		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getRebateRateByIdList_003() {
		Logger.start(true,"代理类型id批量查询返佣比例-验证代理自主继承,返佣为0");
		try {
			createmchBean createmchbean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			int mchId = createmchbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(2);
			createagentBean.setRebateRate(new BigDecimal(0));//返佣为0
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			HashMap map=new HashMap();
			map.put("mchId", mchId);
			List<agent_type> list=agentTypeWapper.selectby(map);
				
			bean.setAgentTypeIdList(Lists.newArrayList(list.get(0).getAgentType(),list.get(1).getAgentType()));
			call.setData(bean);
			call.callService();			
			
			check_verify( bean.getAgentTypeIdList());				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getRebateRateByIdList_005() {
		Logger.start(true,"代理类型id批量查询返佣比例-验证代理类型id列表为空");
		try {
			//bean.setAgentTypeIdList(Lists.newArrayList(list.get(0).getAgentType(),list.get(1).getAgentType()));
			bean.setAgentTypeIdList(null);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatus1(), "验证返回的status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}